﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Data;
using App.Core.Extensions;
using System.Data;

public partial class Pages_Public_CompanyList : BasePage
{
    protected int _BusinessTypeID = 0;
    private String _StartsWith = String.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {            
            BindCustomerList(1);
        }
    }
    protected void BindPageInfo()
    {
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
        _BusinessTypeID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.BUSINESS_TYPE_ID);
        _StartsWith = WebUtil.GetQueryStringInString(AppConstants.QueryString.STARTS_WITH);
        _StartsWith = !_StartsWith.IsNullOrEmpty() && _StartsWith.Length > 1 ? String.Empty : _StartsWith;
        this.Master.ShowServiceProviderMenu = true;
    }    
    protected void BindCustomerList(int pageNumber)
    {        
        ///Bind Alpahbetic Control
        ucAlphabeticControls.URLFormat = Request.Url.AbsolutePath + WebUtil.BuildUrlWithQueryStrings(new List<String>() { AppConstants.QueryString.STARTS_WITH });
        ucAlphabeticControls.URLFormat += ucAlphabeticControls.URLFormat.IndexOf("?") > -1 ? "&" + AppConstants.QueryString.STARTS_WITH + "={0}" : "?" + AppConstants.QueryString.STARTS_WITH + "={0}";
        ucAlphabeticControls.CurrentLetter = _StartsWith;
        //BtcHousingEntities context = new BtcHousingEntities();
        //BusinessType category = context.BusinessTypes.FirstOrDefault(C => C.ID == _BusinessTypeID);
        //if (category != null)
        //    h4Heading.InnerHtml = String.Format("Company List of {0}", category.ShortName);

        //var companies = from P in context.Customers where P.BusinessTypeID == _BusinessTypeID && P.IsDeleted == false && P.IsApproved == true  select P;
        //if (companies == null || companies.Count() == 0)
        //{
        //    ucCustomerList.DataSource = null;
        //    WebUtil.ShowMessageBox(divMessage, String.Format("No Company found for {0}", category == null ? "this category." : category.ShortName), true);
        //}
        //else
        //    ucCustomerList.DataSource = companies.ToList();
        //ucCustomerList.DataBind();
        
         BtcHousingEntities context = new BtcHousingEntities();
        BusinessType category = context.BusinessTypes.FirstOrDefault(C => C.ID == _BusinessTypeID);
        if (category == null)
            WebUtil.ShowMessageBox(divMessage, "Sorry! Requested Company Category was not found.", true);
        else
        {
            h4Heading.InnerHtml = String.Format("Company List of {0}", category.ShortName);
            int totalRecord = 0;
            UtilityDAO dao = new UtilityDAO();
            DataSet ds = dao.SearchCompanies(_BusinessTypeID, _StartsWith, pageNumber, ConfigReader.CompanyListSize, out totalRecord);
            if (ds.IsEmpty() || ds.Tables[0].Rows.Count == 0)
            {
                ucCustomerList.DataSource = null;
                WebUtil.ShowMessageBox(divMessage, String.Format("No Company found for {0}", category == null ? "this category." : category.ShortName), true);
            }
            else
                ucCustomerList.DataSource = ds.Tables[0];

            ucCustomerList.DataBind();

            ///Bind Pager Control
            ucPagerBottom.TotalRecord = totalRecord;
            ucPagerBottom.PageSize = ConfigReader.CompanyListSize;
            ucPagerBottom.PageNo = pageNumber;
            ucPagerBottom.TotalRecordMessage = "Total {0} Company(s) Found.";
            ucPagerBottom.DataBind();
        }
    }
    protected void ucPagerBottom_PageIndexChanged(object sender, PagerEventArgs e)
    {
        BindCustomerList(e.PageIndex);
    }
}